
if ( typeof Skt.Slide === 'undefined' )
	Skt.Slide = {};

Skt.Slide.setup = function(sketchpad) {
	sketchpad.on('editorCanvasesUpdated', Skt.Slide.handleEditorCanvasesUpdated, Skt.Slide);
	sketchpad.on('roomListUpdate', Skt.Slide.handleRoomListUpdate, Skt.Slide);
	sketchpad.on('editorCurrentCanvasChanged', Skt.Slide.handleEditorCurrentCanvasChanged, Skt.Slide);
	sketchpad.on('editorCanvasChanged', Skt.Slide.handleEditorCanvasChanged, Skt.Slide);
};

Skt.Slide.handleEditorCanvasChanged = function() {
	setTimeout(function(){
		Skt.Slide.updateEditorBackground();
	}, 100);
};

Skt.Slide.handleUploadIFrameLoaded = function() {
	var html = $(document.getElementById('upload-files-iframe').contentWindow.document.body.innerHTML).html();
	if ( html != null && html != '' ) {
		var currentRoom = Skt.sketchpad.getCurrentRoom();
		var editor = Skt.sketchpad.getEditor();
		var response = JSON.parse(html);
		
		if ( currentRoom != null 
				&& editor.isEditingEnabled()
				&& currentRoom.getRoomModerator().canEdit(Skt.sketchpad.getUser()) ) {
			currentRoom.addCommand("setBackground", {background: Skt.sketchpad.getBasePath() + '/files/' + response.id + '/' + response.originalName});
		}
	}
};

Skt.Slide.handleEditorCurrentCanvasChanged = function(params) {
	var newCanvas = params.newCanvas;
	var oldCanvas = params.oldCanvas;
	var editor = params.editor;
	
	if ( oldCanvas != null ) {
		Skt.Slide.updateMiniview(oldCanvas);
	}

	$("#editor").html("");
	
	if ( newCanvas != null ) {
		var svgNode = newCanvas.getRaphael().canvas;
		$("#editor").append(svgNode);

		setTimeout(function(){
			Skt.Slide.updateEditorBackground();
			Skt.Slide.updateMiniview(newCanvas);
		}, 100);
	}
	
	editor.setTool(new Skt.SelectTool(editor));
	
	setTimeout(function(){
		Skt.Slide.updateSelectedCanvas();
		Skt.Util.updateEmptyMessage(editor.getSketchpad());
	}, 100);
};

Skt.Slide.handleCanvasChange = function(params) {
	setTimeout(function(){
		Skt.Slide.updateMiniview(params.canvas);
	}, 100);
};

Skt.Slide.handleEditorCanvasesUpdated = function(params) {
	var canvas = params.canvas;
	var room = canvas.getRoom();
	var editor = Skt.sketchpad.getEditor();
	var user = Skt.sketchpad.getUser();
	
	if ( params.action == "addCanvas" ) {
		canvas.on('change', Skt.Slide.handleCanvasChange, Skt.Slide);
		if ( room.getOwnerId() == user.id )
			editor.setCurrentCanvas(room.getId());
	} else if ( params.action == "removeCanvas" ) {
		canvas.off('change');
		if ( room.getId() == editor.getCurrentRoomId() )
			editor.setCurrentCanvas(null);
	}
	
	setTimeout(function(){
		Skt.SlidesSidebar.updateAllCanvasesFromRoomlist(room.getRoomList());
		Skt.Slide.updateSelectedCanvas();
	}, 100);
};

Skt.Slide.handleRoomListUpdate = function(params) {
	if ( params.action == "addRoom" ) {
		Skt.sketchpad.getEditor().addCanvasFromRoom(params.room);
	} else if (params.action == "removeRoom") {
		Skt.sketchpad.getEditor().removeCanvasFromRoom(params.room);
	}
};

Skt.Slide.handleMiniviewClick = function(roomId) {
	//	TODO: Revisar condiciones de seguimiento para poder cambiar de canvas
	//	if ( !ignoreFollowing 
	//	&& Skt.teacherMonitor.isFollowingEnabled()
	//	&& Skt.teacherMonitor.getTeacherRoomId() != null
	//	&& Skt.user.type != 'teacher' ) 
	//{
	//if ( Skt.user.type == 'tv' && (room.getType() != 'screen' || room.getType() != 'question') )
	//	return;
	//else if ( room.getType() == 'screen' || room.getType() == 'question' ) {
	//	if ( this._findCanvas(Skt.teacherMonitor.getTeacherRoomId()) != null ) {
	//		id = Skt.teacherMonitor.getTeacherRoomId();
	//		canvas = this._findCanvas(id);
	//		room = canvas.getRoom();
	//	}
	//}
	//}
	Skt.sketchpad.getEditor().setCurrentCanvas(roomId);
};

Skt.Slide.updateEditorBackground = function() {
	var canvas = Skt.sketchpad.getEditor().getCurrentCanvas();
	
	if ( canvas != null ) {
		var room = canvas.getRoom();
		
		if ( room.getBackground() != null ) {
			$("#editor").css('background-image',"url('" + room.getBackground() + "')");
			
			var pos = canvas.getPanzoom().getCurrentPosition();
			var zoom = canvas.getPanzoom().getCurrentZoom();
			
			var size = (zoom * 11) + 100;
			$('#editor').css('background-size', size + '% ' + size + '%');
			if ( size > 100 )
				$('#editor').css('background-position', (-pos.x*(size/100.0))  + 'px ' + (-pos.y*(size/100.0)) + 'px' );
			else
				$('#editor').css('background-position', '0px 0px' );
			
		} else {
			Skt.Slide.resetEditorBackground();
		}
		
	} else {
		Skt.Slide.resetEditorBackground();
	}
};

Skt.Slide.resetEditorBackground = function() {
	$("#editor").css('background-image',"none");
	$('#editor').css('background-size', '100% 100%');
	$('#editor').css('background-position', '0px 0px' );
};

Skt.Slide.updateSelectedCanvas = function() {
	$(".room").removeClass('selected');
	var canvas = Skt.sketchpad.getEditor().getCurrentCanvas();
	if ( canvas != null )
		$("#room-" + canvas.getRoom().getId()).addClass('selected');
};

Skt.Slide.updateMiniview = function(canvas) {
	var room = canvas.getRoom();
	var roomId = room.getId();
	var svgNode = canvas.getSVGNode()[0];
	var clone = svgNode.cloneNode(true);
	
	clone.style.left = 0;
	clone.style.top = 0;
	clone.setAttribute('width', 102.4);
	clone.setAttribute('height', 67.2);
	clone.setAttribute('viewBox', '0 0 1024 672');
	
	$("#room-" + roomId).html("");
	$("#room-" + roomId).append(clone);
	
	if ( room.getBackground() != null )
		$("#room-" + roomId).css('background-image',"url('" + room.getBackground() + "')");
	else
		$("#room-" + roomId).css('background-image',"none");
};

